
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.ListOperations;
import com.sys.midware.redis.exceptions.BRedisException;
import com.sys.midware.redis.util.SerializationUtils;

import redis.clients.jedis.JedisCluster;

/**
 * <b>Description：</b> List 操作类 <br/>
 * <b>ClassName：</b> ListOperationsImpl <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:11:23 <br/>
 * <b>@version: </b>  <br/>
 */
public class ClusterListOperationsImpl extends AbstractOperations implements ListOperations{

    private static Logger logger = Logger.getLogger(StringsOperationsImpl.class);
    private ClusterListOperationsImpl() {
    };

    private static ClusterListOperationsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static ClusterListOperationsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (ClusterListOperationsImpl.class) {
            if (instance == null) {
                instance = new ClusterListOperationsImpl();
            }
            return instance;
        }
    }
    @Override
    public <K, T> Long putList(K key, List<T> values) {
        
        JedisCluster jedis = getClient4Cluster();
        try {
            Long l = null;
            if(!jedis.exists(rawKey(key))){
                l = jedis.rpush(rawKey(key), rawValues(values));
            }else{
                l =jedis.rpush(rawKey(key), rawValues(values));
            }
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public <K, V> List<V> getFromListByPage(K key, int start, int end) {
        JedisCluster jedis = getClient4Cluster();
      try {
        List<byte[]> re=  jedis.lrange(rawKey(key), start, end);
          return SerializationUtils.deserialize(re);
      } catch (Exception e) {
          logger.error(e);
          throw new BRedisException(e);
      } finally {
          RedisFactory.close(jedis);
      }
    }
    @Override
    public <K, V> List<V> getFromList(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
          List<byte[]> re=  jedis.lrange(rawKey(key), 0, -1);
            return SerializationUtils.deserialize(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long push(K key, V value) {
        JedisCluster jedis = getClient4Cluster();
        try {
            Long re=  jedis.lpush(rawKey(key), rawValue(value));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> V pop(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            byte[] re=  jedis.lpop(rawKey(key));
            return deserializeValue(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long in(K key, V value) {
        JedisCluster jedis = getClient4Cluster();
        try {
            Long re=  jedis.rpush(rawKey(key), rawValue(value));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> V out(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            byte[] re=  jedis.lpop(rawKey(key));
            return deserializeValue(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> Long size(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            Long re=  jedis.llen(rawKey(key));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public  boolean delete(Object... keys) {
        JedisCluster jedis = getClient4Cluster();
        boolean flag =false;
        try {
                jedis.del(rawKeys(keys));
            flag = true;
            return flag;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
        
    }

    @Override
    public <K, V> V index(K key, long index) {
        JedisCluster jedis = getClient4Cluster();
        try {
            byte[] re=  jedis.lindex(rawKey(key), index);
            return deserializeValue(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> String set(K key, long index, V value) {
        JedisCluster jedis = getClient4Cluster();
        try {
           String re=  jedis.lset(rawKey(key), index, rawValue(value));
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
        
    }

    @Override
    public <K, V> String trim(K key, long start, long end) {
        JedisCluster jedis = getClient4Cluster();
        try {
           String re=  jedis.ltrim(rawKey(key), start, end);
            return re;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    
}
